package com.whh.dota2helper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;

import com.whh.dota2helper.service.HeroService;

public class SplashScreen extends Activity {
	protected boolean _active = true;
	protected int _splashTime = Integer.MAX_VALUE;
	private HeroService heroService ;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_screen);
		heroService = new HeroService(SplashScreen.this);
		
		Thread splashTread = new Thread() {
			@Override
			public void run() {
				try {
					int waited = 0;
					while (_active && (waited < _splashTime)) {
						sleep(1000);
						if (_active) {
							waited += 1000;
						}
					}
				} catch (InterruptedException e) {
					// do nothing
				} finally {
					finish();
					// 启动主应用
					startActivity(new Intent(SplashScreen.this,HeroListActivity.class));
					try{
						stop();
					}catch(Exception e){
						
					}
				}
			}
		};
		splashTread.start();
		
		if(!heroService.isExist()){
			Thread loadThread = new Thread(){
				@Override
				public void run() {
					heroService.insertAll();
					_active = false;
				}
			};
			loadThread.start();
		}else{
			_active = false;
		}
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			//_active = false;
		}
		return true;
	}
}
